//
//  ProductViewController.m
//  Slice
//
//  Created by lanou on 16/6/15.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "ProductViewController.h"

@interface ProductViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ProductViewController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)getData {
    [RequestTool requsetWithType:POST URLString:kProductListURL paremeter:@{@"auth":@""} callBack:^(NSData *data, NSError *error) {
        if (data == nil) {
            return ;
        }
        
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic  in dataDic[@"data"][@"list"]) {
            [self.dataArray addObject:[ProductListModel modelWithDiction:dic]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
}






- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"良品";
    
    [self getData];
    
    self.view.backgroundColor = [UIColor blueColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ProductTableViewCell" bundle:nil] forCellReuseIdentifier:@"123"];
}

#pragma mark --------代理方法-----

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (0.6 * kWidth + 65);
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    [cell getDataByModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
